package qp.src.app.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class EntityService{

  private static DataBeanDao dataBeanDao;
  private static SubDataBeanDao subDataBeanDao;

  public static SubDataBean findSubDataBean(Integer id){
    return subDataBeanDao.findSubDataBean(id);
  }

  public static DataBean findDataBean(Integer id){
    System.out.println("--Server EntityService findDataBean ");
    return dataBeanDao.findDataBean(id);
  }

  public static List<DataBean> getDataBean(){
    System.out.println("--Server EntityService getDataBean dataBeanDao"+dataBeanDao);
    checkDataDao();
    return dataBeanDao.getDataBean();
  }

  public static void delete(DataBean dataBean) {
    System.out.println("--SERVER EntityService delete");
    checkDataDao();
    dataBeanDao.delete(dataBean);
  }

  public static void persist(DataBean dataBean) {
    System.out.println("--SERVER EntityService persist");
    checkDataDao();
    dataBeanDao.persist(dataBean);
  }

  private static void checkDataDao(){
    if( dataBeanDao == null ){
      init();
    }
  }


  private static void init() {
    System.out.println("--SERVER EntityService init");
    List<DataBean> datas = initFakeDataBean();

    subDataBeanDao = new SubDataBeanDao( datas );
    dataBeanDao = new DataBeanDao( datas, subDataBeanDao );
  }

  private static List<DataBean> initFakeDataBean(){
    List<DataBean> data = new ArrayList<DataBean>();
    for(int i = 0; i < 5; i++){
      DataBean dataBean = new DataBean();
      dataBean.setAddress("New York"+i);
      dataBean.setLastName("Coolper"+i);
      dataBean.setName("Bill"+i);
      dataBean.setDateTime( new Date() );

      dataBean.setData( initFakeSubDataBean( i ) );
      data.add( dataBean );
    }
    return data;
  }

  private static SubDataBean initFakeSubDataBean(Integer id){
    SubDataBean subDataBean = new SubDataBean();
    subDataBean.setLastName("Sub Sur"+id);
    subDataBean.setName("Sub Name"+id);
    return subDataBean;
  }



}
